<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

</head>

<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-lg-offset3" layui-col-lg6>
            <div class="layui-form layuimini-form">
                <form class="layui-form" action="" lay-filter="userForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户id：</label>
                        <div class="layui-input-block">
                            <input type="text" name="userId" value="" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required"
                                   autocomplete="off" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">此次消费金额为(含打包费)：</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" lay-verify="required"
                                   autocomplete="off" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">消费地点：</label>
                        <div class="layui-input-block">
                            <input type="text" name="resname" id="resname" lay-verify="required"
                                   autocomplete="off" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">用餐方式为：</label>
                        <div class="layui-input-block">
                            <input type="text" name="outstring" id="outstring" lay-verify="required"
                                   autocomplete="off" class="layui-input" readonly="readonly">
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">支付密码：</label>
                        <div class="layui-input-block">
                            <input type="password" name="paypassword" id="paypassword" lay-verify="required" placeholder="请输入密码"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <input type="hidden" name="out" value="" class="layui-input" readonly="readonly">
                    <input type="hidden" name="menuId" value="" class="layui-input" readonly="readonly">
                    <input type="hidden" name="foodId" value="" class="layui-input" readonly="readonly">

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
    </div>
</div>



<script>
    //Demo
    layui.use(['form','upload','element'], function () {
        // 表单对象  form
        var form = layui.form
            //jquery 对象 $
            , $ = layui.jquery
            ,element = layui.element
            ,upload = layui.upload
            // 日期组件
            , laydate = layui.laydate;

        //日期   渲染上面 id 为 date 的 标签
        laydate.render({
            elem: '#date'
        });

        // 从 localStorage 把数据取出来   并转成 js 对象
        //  JSON.parse("str")   把字符串转为 js 对象
        var data = JSON.parse(localStorage.getItem("User"));
        var data1 = JSON.parse(localStorage.getItem("allprice"));
        var data2 = JSON.parse(localStorage.getItem("menuId"));
        var out = JSON.parse(localStorage.getItem("out"));
        var res= JSON.parse(localStorage.getItem("res"));
        var resname= JSON.parse(localStorage.getItem("resname"));
        var outstring;
        if (out==1){
            outstring="外带"
        }else if (out==0){
            outstring="堂食"
        };

        //给表单数据赋值
        form.val("userForm",{
            "name":data.username,
            "money":data1,
            "userId":data.id,
            "menuId":data2,
            "foodId":res,
            "out":out,
            "outstring":outstring,
            "resname":resname,
        });


        //监听提交
        form.on('submit(formDemo)', function (data) {  //data指代的是form 表单

            console.log(JSON.stringify(data.field));

            $.ajax({
                "url":"http://localhost:8080/canteen/pay",
               // "headers":{"token":token},
                "data": data.field,  //
                // result 参数 表式后台响应的数据
                "success":function(result){
                    var str =  JSON.stringify(result);

                    console.log("----------------"+str);
                    if(result.code == 200){
                        // 关闭 弹出层
                        layer.msg(result.data);

                        setTimeout(function(){//设置定时器
                            parent.location.reload(true);//刷新父级页面
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        },2000);

                    }else{
                        layer.msg(result.message);
                    }
                },
                "dataType":"json"
            });

            return false;
        });
    });
</script>


</body>

</html>